package com.athuahua.spzx.controller;


import com.athuahua.spzx.exception.CodeException;
import com.athuahua.spzx.utils.AliOssUtil;
import com.athuahua.spzx.utils.Result;
import com.athuahua.spzx.utils.ResultCodeEnum;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

/**
 * @Author: huahua
 * @Date: 2024/5/9 0009
 * @Time: 17:08
 * @Description: 通用接口
 */
@RestController
@RequestMapping("admin/system")
@Tag(name = "通用接口")
@Slf4j
public class CommonController {

    @Resource
    private AliOssUtil aliOssUtil;

    @Operation(summary = "文件上传接口")
    @PostMapping("fileUpload")
    public Result<String> upload(@RequestParam("file") MultipartFile file) {
        log.info("文件上传: {}", file);


        try {
            String originalFilename = file.getOriginalFilename();
            //截取文件名后缀
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            String objectName = UUID.randomUUID().toString() + substring;

            String fillPath = aliOssUtil.upload(file.getBytes(), objectName);
            return Result.success(fillPath);


        } catch (IOException e) {
            log.error("文件上传失败：{}",e);
            throw new CodeException(ResultCodeEnum.DATA_ERROR);
        }


    }
}
